<script setup lang="ts">
import type { VideoParams } from '@/types/video'
import { ElNotification, ElMessageBox } from 'element-plus'
import { ref, onMounted, watch } from 'vue'
import { checkVideo, getVideoInfo, delVideo } from '@/api/video'
import { formatDateFull } from '@/utils/format'

// 获取视频数据
const videoData = ref<VideoParams[]>([])
const loading = ref(false)
const videoRef = ref()
// 分页
const total = ref(0)
const params = ref({
  // 当前页
  page: 1,
  // 当前生效的每页条数
  pageSize: 10,
  // 审核状态
  status: '',
  // 关键字
  key: ''
})
// 条件查询
const options = [
  {
    value: 0,
    label: '待审核'
  },
  {
    value: 1,
    label: '审核通过'
  },
  {
    value: 2,
    label: '审核未通过'
  }
]
const getVideo = async () => {
  loading.value = true
  const result = await getVideoInfo(params)
  videoData.value = result.data
  // @ts-ignore
  total.value = result.total
  videoData.value.map(
    (item: VideoParams) => (item.create_time = formatDateFull(item.create_time))
  )
  videoData.value = videoData.value.filter((item) => item.status !== 3)
  loading.value = false
}
const handleSizeChange = (size: number) => {
  params.value.page = 1
  params.value.pageSize = size
  getVideo()
}
const handleCurrentChange = (value: number) => {
  params.value.page = value
  getVideo()
}
const searchBtn = () => {
  getVideo()
}
const resetBtn = () => {
  params.value.status = ''
  params.value.key = ''
  getVideo()
}

// 是否显示抽屉
const videoDetail = ref<VideoParams>({} as VideoParams)
const isShow = ref(false)
const selectDetail = (row: VideoParams) => {
  isShow.value = true
  videoDetail.value = row
}
// 视频审核
const btnOK = async (flag: number) => {
  videoDetail.value.status = flag
  await checkVideo(videoDetail.value)
  isShow.value = false
  ElNotification({
    title: '温馨提示',
    message: '审核完成',
    type: 'success',
    duration: 1500
  })
  getVideo()
}
const btnCheckClose = async (row: VideoParams, flag: number) => {
  videoDetail.value = row
  btnOK(flag)
}
// 删除视频
const delBtn = async (id: number) => {
  await ElMessageBox.confirm('你确定要删除该视频吗？', '温馨提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning'
  })
  await delVideo(id)
  ElNotification({
    title: '温馨提示',
    message: '删除成功',
    type: 'success',
    duration: 1500
  })
  getVideo()
}

watch(isShow, (value) => {
  if (value === false) {
    videoRef.value.pause()
  }
})

onMounted(() => {
  getVideo()
})
</script>

<template>
  <page-container title="视频管理">
    <!-- 表单 -->
    <el-form style="display: flex">
      <el-form-item label="关键词：">
        <el-input
          v-model="params.key"
          placeholder="请输入关键词"
          style="width: 240px; margin-right: 20px"
        />
      </el-form-item>
      <el-form-item label="审核状态：">
        <el-select
          v-model="params.status"
          placeholder="请选择"
          style="width: 240px; margin-right: 20px"
        >
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="">
        <el-button type="primary" @click="searchBtn">搜索</el-button>
        <el-button @click="resetBtn">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 表格 -->
    <el-table :data="videoData" v-loading="loading" style="width: 100%">
      <el-table-column type="index" label="序号" width="100" align="center" />
      <el-table-column label="标题" align="center">
        <template #default="{ row }">
          <span class="text">{{ row.title }}</span>
        </template>
      </el-table-column>
      <el-table-column label="视频" align="center">
        <template #default="{ row }">
          <div style="display: flex; justify-content: center">
            <el-image :src="row.cover" style="width: 144px; height: 80px" />
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" align="center">
        <template #default="{ row }">
          <span v-if="row.status === 0">待审核</span>
          <span v-if="row.status === 1">审核通过</span>
          <span v-if="row.status === 2">审核未通过</span>
        </template>
      </el-table-column>
      <el-table-column prop="create_time" label="创建时间" align="center" />
      <el-table-column label="操作" align="center">
        <template #default="{ row }">
          <el-button type="primary" link @click="selectDetail(row)"
            >查看</el-button
          >
          <template v-if="row.status !== 0">
            <el-button type="primary" link @click="delBtn(row.id)"
              >删除</el-button
            >
          </template>
          <template v-if="row.status === 0">
            <el-button type="primary" link @click="btnCheckClose(row, 1)"
              >通过</el-button
            >
            <el-button type="primary" link @click="btnCheckClose(row, 2)"
              >驳回</el-button
            >
          </template>
        </template>
      </el-table-column>
      <!-- 暂无数据 -->
      <template #empty>
        <el-empty description="暂无数据" />
      </template>
    </el-table>

    <!-- 分页 -->
    <el-row
      style="display: flex; justify-content: end"
      v-if="videoData.length > 0"
    >
      <el-pagination
        v-model:current-page="params.page"
        v-model:page-size="params.pageSize"
        :page-sizes="[2, 3, 5, 10]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        style="margin-top: 20px"
      />
    </el-row>
  </page-container>

  <!-- 抽屉 - 视频 -->
  <el-drawer v-model="isShow" title="视频详情" direction="rtl" size="36%">
    <el-form :model="videoDetail" label-width="50px" style="padding: 10px">
      <el-form-item label="标题" prop="title">
        <el-input v-model="videoDetail.title" disabled></el-input>
      </el-form-item>
      <el-form-item
        label="简介"
        prop="introduction"
        v-if="videoDetail.introduction"
      >
        <el-input
          v-model="videoDetail.introduction"
          disabled
          type="textarea"
          autosize
        ></el-input>
      </el-form-item>
      <el-form-item label="视频">
        <video
          ref="videoRef"
          controls
          :src="videoDetail.video"
          :poster="videoDetail.cover"
        ></video>
      </el-form-item>
      <el-form-item>
        <template v-if="videoDetail.status === 0">
          <el-button type="primary" @click="btnOK(1)">通过</el-button>
          <el-button @click="btnOK(2)">驳回</el-button>
        </template>
        <template v-if="videoDetail.status === 1">
          <el-button type="primary" disabled>已通过</el-button>
        </template>
        <template v-if="videoDetail.status === 2">
          <el-button disabled>已驳回</el-button>
        </template>
      </el-form-item>
    </el-form>
  </el-drawer>
</template>

<style scoped lang="scss">
.text {
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
video {
  width: 100%;
  border-radius: 8px;
}
</style>
